﻿function onSignIn(googleUser) {
    // Useful data for your client-side scripts:
    var profile = googleUser.getBasicProfile();
    var id_token = googleUser.getAuthResponse().id_token;
    $("#token").val(id_token);
    $("#formGoogle").submit();
    //$.post(window.singleConfig.rootUrl + "thirdparty/google", { "token": id_token, }, function (data, status) {
    //    if (data.code == "success") {
    //        layer.msg(data.message);
    //        setTimeout(function () {
    //            window.location.href = data.redirectUrl;
    //        }, 300);
    //    } else {
    //        layer.msg(data.message);
    //    }
    //}, "json");
};

function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
        layer.msg('退出成功');
    });
}

$(document).ready(function () {
    gapi.load('auth2,signin2', function () {
        var auth2 = gapi.auth2.init();
        auth2.then(function () {
            // Current values
            var isSignedIn = auth2.isSignedIn.get();
            var currentUser = auth2.currentUser.get();

            if (!isSignedIn) {
                // Rendering g-signin2 button.
                gapi.signin2.render('google-signin-button', {
                    'onsuccess': 'onSignIn'
                });
            }
            else {
                var id_token = currentUser.getAuthResponse().id_token;
                $("#token").val(id_token);
                $("#formGoogle").submit();
                //setTimeout(function () {
                //    $.post(window.singleConfig.rootUrl + "thirdparty/google", { "token": id_token, }, function (data, status) {
                //        if (data.code == "success") {
                //            layer.msg(data.message);
                //            setTimeout(function () {
                //                window.location.href = data.redirectUrl;
                //            }, 300);
                //        } else {
                //            layer.msg(data.message);
                //        }
                //    }, "json");
                //}, 1500);
            }
        });
    });
});